﻿using PagedList;
using PagedList.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GiaoTrinhEPU.Repositories.Repositories;
using GiaoTrinhEPU.Repositories.DataModels;
using GiaoTrinhEPU.Repositories;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Core.ActionResults;
using GiaoTrinhEPU.Core;

namespace GiaoTrinhEPU.Controllers
{
    public class AuthorController : BaseController
    {
        /// <summary>
        /// Declare Author Repository
        /// </summary>
        private AuthorRepository authorRepository;
        
        /// <summary>
        ///  Declare Session
        /// </summary>
        public static string SessionAuthorSort = "SessionAuthorSort";
        public static string SessionAuthorSearch = "SessionAuthorSearch";
        public static string SessionAuthorSortOpt = "SessionAuthorSortOpt";



        // GET: /Author/
        /// <summary>
        /// Constructor that allow to create a Comment controller
        /// </summary>
        public AuthorController()
            : base()
        {
            this.authorRepository = new AuthorRepository();
            ViewBag.SelectedMenuId = 25;
            
        }

        public ActionResult Index(int page = 1, int size = 25, String search = "", String opt = "", String sort = "")
        {
            ViewBag.Title = "Tác Giả";
            // Stored Session
            Session[SessionAuthorSort] = sort.Trim().Length > 0 ? sort.ToLower() : Session[SessionAuthorSort];
            Session[SessionAuthorSortOpt] = opt.Trim().Length > 0 ? opt.ToLower() : Session[SessionAuthorSortOpt];

            var author = this.authorRepository.ListAuthors(false).ToList();

            // Sort 
            ViewBag.SortNameOpt = "desc";
            if (Session[SessionAuthorSort] != null)
            {
                switch (Session[SessionAuthorSort].ToString())
                {
                    case "name":
                        author = Session[SessionAuthorSortOpt].ToString().Equals("desc") ? author.OrderByDescending(s => s.Name).ToList() : author.OrderBy(s => s.Name).ToList();

                        Session[SessionAuthorSort] = "name";
                        ViewBag.SortNameOpt = Sort.SetSortOrderOption(Session[SessionAuthorSortOpt].ToString());
                        break;

                    default:
                        break;
                }
            }


            return View(author.ToPagedList(page, size));
        }
      
    }
}
